package com.wechat.DAO.DaoImpl;

import com.wechat.DAO.FriendRequestDAO;
import com.wechat.pojo.FriendRequest;
import com.wechat.pojo.Message;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class FriendRequestDAOIpml implements FriendRequestDAO {
    Connection connection = null;
    PreparedStatement pst = null;
    ResultSet resultSet = null;
    @Override
    public FriendRequest queryByBothID(String SID, String RID) {
        FriendRequest friendRequest = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/wechat?useSSL=false", "root", "123456");
            PreparedStatement pst = connection.prepareStatement("SELECT * FROM FRIENDREQUEST WHERE SID=? AND RID=?");
            pst.setString(1,SID);
            pst.setString(2,RID);
            resultSet = pst.executeQuery();
            while (resultSet.next()){
                friendRequest = new FriendRequest(
                        resultSet.getLong("M_Time"),
                        resultSet.getString("SID"),
                        resultSet.getString("Snickname"),
                        resultSet.getString("RID"),
                        resultSet.getString("text"),
                        resultSet.getString("HPUrl"),
                        resultSet.getString("status"),
                        resultSet.getBoolean("IF_Receive"));
            }
            System.out.println(friendRequest);
            }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            closeAll();
            if(resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return friendRequest;
    }

    @Override
    public List<FriendRequest> queryByRID(String RID) {
        List<FriendRequest> friendRequests = new ArrayList<>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/wechat?useSSL=false", "root", "123456");
            PreparedStatement pst = connection.prepareStatement("SELECT * FROM FRIENDREQUEST WHERE RID=?");
            pst.setString(1,RID);
            resultSet = pst.executeQuery();
            while (resultSet.next()){
                System.out.println(resultSet.getBoolean("IF_Receive"));
//                FriendRequest friendRequest = new FriendRequest()
                FriendRequest friendRequest = new FriendRequest(
                        resultSet.getLong("M_Time"),
                        resultSet.getString("SID"),
                        resultSet.getString("Snickname"),
                        resultSet.getString("RID"),
                        resultSet.getString("text"),
                        resultSet.getString("HPUrl"),
                        resultSet.getString("status"),
                        resultSet.getBoolean("IF_Receive"));
                friendRequests.add(friendRequest);
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            closeAll();
            if(resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return friendRequests;
    }

    @Override
    public List<FriendRequest> queryBySID(String SID) {
        List<FriendRequest> friendRequests = new ArrayList<>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/wechat?useSSL=false", "root", "123456");
            PreparedStatement pst = connection.prepareStatement("SELECT * FROM FRIENDREQUEST WHERE SID=?");
            pst.setString(1,SID);
            resultSet = pst.executeQuery();
            while (resultSet.next()){
                System.out.println(resultSet.getBoolean("IF_Receive"));
                FriendRequest friendRequest = new FriendRequest(
                        resultSet.getLong("M_Time"),
                        resultSet.getString("SID"),
                        resultSet.getString("Snickname"),
                        resultSet.getString("RID"),
                        resultSet.getString("text"),
                        resultSet.getString("HPUrl"),
                        resultSet.getString("status"),
                        resultSet.getBoolean("IF_Receive"));
                friendRequests.add(friendRequest);
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            closeAll();
            if(resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return friendRequests;
    }

    @Override
    public Boolean insert(FriendRequest friendRequest) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/wechat?useSSL=false", "root", "123456");
            PreparedStatement pst = connection.prepareStatement("INSERT INTO FRIENDREQUEST VALUE (?,?,?,?,?,?,?,?)");
            pst.setLong(1,friendRequest.getM_Time());
            pst.setString(2,friendRequest.getSID());
            pst.setString(3,friendRequest.getSnickname());
            pst.setString(4,friendRequest.getRID());
            pst.setString(5,friendRequest.getText());
            pst.setString(6,friendRequest.getHPUrl());
            pst.setString(7,friendRequest.getStatus());
            pst.setBoolean(8,friendRequest.getIF_Receive());
            pst.execute();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            closeAll();
        }
        return true;
    }

    @Override
    public Boolean receiveOk(String SID, String RID) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/wechat?useSSL=false", "root", "123456");
            PreparedStatement pst = connection.prepareStatement("UPDATE FRIENDREQUEST SET IF_RECEIVE=1 WHERE SID=? AND RID=?");
            pst.setString(1,SID);
            pst.setString(2,RID);
            pst.execute();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            closeAll();
        }
        return true;
    }

    @Override
    public Boolean delete(String SID, String RID) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/wechat?useSSL=false", "root", "123456");
            PreparedStatement pst = connection.prepareStatement("DELETE FROM FRIENDREQUEST WHERE SID=? AND RID=?");
            pst.setString(1,SID);
            pst.setString(1,RID);
            pst.execute();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            closeAll();
        }
        return true;
    }

    @Override
    public Boolean updateStatus(String SID, String RID, String status) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/wechat?useSSL=false", "root", "123456");
            PreparedStatement pst = connection.prepareStatement("UPDATE FRIENDREQUEST SET STATUS=? WHERE SID=? AND RID=?");
            pst.setString(1,status);
            pst.setString(2,SID);
            pst.setString(3,RID);
            pst.execute();
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally {
            closeAll();
        }
        return true;
    }


    private void closeAll() {
        if(pst!=null){
            try {
                pst.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
